<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<div class="modal fade" id="selectempModal" tabindex="-1" role="dialog" aria-labelledby="selectempModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="selectempModalLabel">选择员工</h4>
            </div>
            <div class="modal-body">
            	<form action="" class="form-horizontal" role="form">
					<div class="form-group">
						<label for="dept" class="col-md-4 control-label">所属部门</label>
						<div class="col-md-5">
							<select name="dept" class="form-control" id="dept">
								<option value="">--请选择--</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="job" class="col-md-4 control-label">所属职位</label>
						<div class="col-md-5">
							<select name="job" class="form-control" id="job">
								<option value="">--请选择--</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label for="emp" class="col-md-4 control-label">员工信息</label>
						<div class="col-md-5">
							<select name="emp" class="form-control" id="emp" size="10">
							</select>
						</div>
					</div>
            	</form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="sub" class="btn btn-primary">确认选择</button>
            </div>
        </div>
    </div>
</div>

<script>
	$(document).ready(function(){
		$.getJSON("dept/listDeptUseSelect",function(listdept){
			var dept = $("#dept")[0];
			dept.options.length=1;
			for(var i=0;i<listdept.length;i++){
				var d = listdept[i];
				dept.options.add(new Option(d.deptName,d.oid));
			}
		}).complete(function(xhr) {
			if(xhr.status=="403"){//没有权限
				document.location.href = xhr.getResponseHeader("url");
			}else if(xhr.status!="200"){
				console.log("服务器错误!");
			}
		});
		
		$("#dept").change(function(){
			if(this.value==""){
				$("#job")[0].options.length=1;
				$("#emp")[0].options.length=0;
				return;
			}
			$.getJSON("dept/listDeptJobUseSelect",{deptid:this.value},function(listjob){
				var job = $("#job")[0];
				job.options.length=1;
				for(var i=0;i<listjob.length;i++){
					var job1 = listjob[i];
					job.options.add(new Option(job1.name,job1.oid));
				}
			}).complete(function(xhr) {
				if(xhr.status=="403"){//没有权限
					document.location.href = xhr.getResponseHeader("url");
				}else if(xhr.status!="200"){
					console.log("服务器错误!");
				}
			});
			
			$.getJSON("dept/listEmpUseSelete",{deptid:$("#dept").val()},function(listEmps){
				var emp = $("#emp")[0];
				emp.options.length=0;
				for(var i=0;i<listEmps.length;i++){
					var e = listEmps[i];
					emp.options.add(new Option(e.empName,e.oid));
				}
			}).complete(function(xhr) {
				if(xhr.status=="403"){//没有权限
					document.location.href = xhr.getResponseHeader("url");
				}else if(xhr.status!="200"){
					console.log("服务器错误!");
				}
			});
		});
		
		$("#job").change(function(){
			$.getJSON("dept/listEmpUseSelete",{deptid:$("#dept").val(),jobid:this.value},function(listEmps){
				var emp = $("#emp")[0];
				emp.options.length=0;
				for(var i=0;i<listEmps.length;i++){
					var e = listEmps[i];
					emp.options.add(new Option(e.empName,e.oid));
				}
			}).complete(function(xhr) {
				if(xhr.status=="403"){//没有权限
					document.location.href = xhr.getResponseHeader("url");
				}else if(xhr.status!="200"){
					console.log("服务器错误!");
				}
			});
		});
		
		$("#sub").click(function(){
			var eid=$("#emp").val();
			if(eid!=null){
				var empName=$("#emp option:selected").text();
				setempvalue(eid,empName);
				$('#selectempModal').modal('hide');
			}else{
				alert("请选择员工");
			}
			
		});
		
		$('#selectempModal').on('show.bs.modal',function() {
			$("#dept").val("");
			$("#job")[0].options.length=1;
			$("#emp")[0].options.length=0;
		});
	});
</script>